<template>
  <div class="basic-data-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>基础数据</span>
        <el-button 
          type="success" 
          size="small" 
          icon="el-icon-download"
          style="float: right;"
          @click="exportData"
          :loading="exportLoading">
          导出CSV
        </el-button>
      </div>
      
      <!-- 筛选条件 -->
      <div class="filter-container">
        <el-form :inline="true" :model="dataFilter" class="demo-form-inline">
          <el-form-item label="渠道">
            <el-select v-model="dataFilter.channel" placeholder="请选择渠道" style="width: 150px;" clearable>
              <el-option
                v-for="item in channelOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="时间范围">
            <el-date-picker
              v-model="dataFilter.timeRange"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchData">查询</el-button>
            <el-button @click="resetFilter">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <!-- 数据表格 -->
      <el-table :data="tableData" border style="width: 100%" v-loading="loading">
        <el-table-column prop="create_time" label="日期" width="120"></el-table-column>
        <el-table-column prop="channel" label="渠道" width="80"></el-table-column>
        <el-table-column prop="click" label="落地页点击次数" width="140"></el-table-column>
        <el-table-column prop="download" label="下载" width="100"></el-table-column>
        <el-table-column prop="register" label="注册" width="100"></el-table-column>
        <el-table-column prop="new" label="新增" width="100"></el-table-column>
        <el-table-column prop="active" label="活跃" width="100"></el-table-column>
        <el-table-column prop="first_recharge" label="首日充值人数" width="130"></el-table-column>
        <el-table-column prop="recharge" label="充值人数" width="120"></el-table-column>
        <el-table-column prop="total_recharge" label="总充值" width="120"></el-table-column>
        <el-table-column prop="total_cashout" label="总提" width="120"></el-table-column>
        <el-table-column prop="total_diff" label="差值" width="120"></el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.page_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { registerStatistic } from '@/api/user'
import { exportDataToCsv } from '@/utils/export'
import channelMixin from '@/mixins/channelMixin'

export default {
  name: 'BasicData',
  mixins: [channelMixin],
  data() {
    return {
      // 筛选条件
      dataFilter: {
        channel: null,
        timeRange: []
      },
      
      // 表格数据
      tableData: [],
      loading: false,
      exportLoading: false,
      
      // 分页信息
      pagination: {
        page: 1,
        page_size: 20,
        total: 0
      }
    }
  },
  
  async mounted() {
    this.loadData()
  },
  
  methods: {
    // 加载数据
    async loadData() {
      this.loading = true
      try {
        const params = {
          channel: this.formatChannelParam(this.dataFilter.channel),
          page: this.pagination.page,
          page_size: this.pagination.page_size
        }
        
        // 处理时间范围
        if (this.dataFilter.timeRange && this.dataFilter.timeRange.length === 2) {
          params.begin_time = this.dataFilter.timeRange[0]
          params.end_time = this.dataFilter.timeRange[1]
        }
        
        const response = await registerStatistic(params)
        this.tableData = response.data.registers || []
        this.pagination.total = (response.data.total_page || 1) * this.pagination.page_size
      } catch (error) {
        this.$message.error('获取基础数据失败')
        console.error(error)
      } finally {
        this.loading = false
      }
    },
    
    // 查询数据
    searchData() {
      this.pagination.page = 1
      this.loadData()
    },
    
    // 重置筛选条件
    resetFilter() {
      this.dataFilter = {
        channel: null,
        timeRange: []
      }
      this.searchData()
    },
    
    // 分页大小改变
    handleSizeChange(val) {
      this.pagination.page_size = val
      this.loadData()
    },
    
    // 当前页改变
    handleCurrentChange(val) {
      this.pagination.page = val
      this.loadData()
    },

    // 导出CSV
    async exportData() {
      this.exportLoading = true
      try {
        const params = {
          channel: this.formatChannelParam(this.dataFilter.channel),
          page: 1,
          page_size: 10000
        }
        
        if (this.dataFilter.timeRange && this.dataFilter.timeRange.length === 2) {
          params.begin_time = this.dataFilter.timeRange[0]
          params.end_time = this.dataFilter.timeRange[1]
        }
        
        const response = await registerStatistic(params)
        const allData = response.data.registers || []
        
        const columns = [
          { prop: 'create_time', label: '日期' },
          { prop: 'channel', label: '渠道' },
          { prop: 'click', label: '落地页点击次数' },
          { prop: 'download', label: '下载' },
          { prop: 'register', label: '注册' },
          { prop: 'new', label: '新增' },
          { prop: 'active', label: '活跃' },
          { prop: 'first_recharge', label: '首日充值人数' },
          { prop: 'recharge', label: '充值人数' },
          { prop: 'total_recharge', label: '总充值' },
          { prop: 'total_cashout', label: '总提' },
          { prop: 'total_diff', label: '差值' }
        ]
        
        const now = new Date()
        const dateStr = now.getFullYear() + 
          String(now.getMonth() + 1).padStart(2, '0') + 
          String(now.getDate()).padStart(2, '0') + '_' +
          String(now.getHours()).padStart(2, '0') + 
          String(now.getMinutes()).padStart(2, '0')
        
        const fileName = `基础数据_${dateStr}.csv`
        exportDataToCsv(allData, columns, fileName)
        this.$message.success('导出成功！')
      } catch (error) {
        this.$message.error('导出失败')
        console.error(error)
      } finally {
        this.exportLoading = false
      }
    }
  }
}
</script>

<style scoped>
.basic-data-container {
  padding: 20px;
}

.filter-container {
  margin-bottom: 20px;
  padding: 20px;
  background: #f5f5f5;
  border-radius: 4px;
}

.demo-form-inline .el-form-item {
  margin-right: 10px;
}

.el-pagination {
  text-align: center;
  margin-top: 20px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
</style> 